﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class ComplexNumber
{
    private double realPart;
    private double imagPart;

    public ComplexNumber(double realPart, double imagPart)
    {
        RealPart = realPart;
        ImagPart = imagPart;
    }
    public ComplexNumber()
        : this(0, 0)
    { }

    public static ComplexNumber operator +(ComplexNumber first, ComplexNumber second)
    {
        double rP = first.RealPart + second.RealPart;
        double iP = first.ImagPart + second.ImagPart;
        return new ComplexNumber(rP, iP);
    }
    public static ComplexNumber operator -(ComplexNumber first, ComplexNumber second)
    {
        double rP = first.RealPart - second.RealPart;
        double iP = first.ImagPart - second.ImagPart;
        return new ComplexNumber(rP, iP);
    }
    public static ComplexNumber operator *(ComplexNumber first, ComplexNumber second)
    {
        double rP = first.RealPart * second.RealPart - first.ImagPart * second.ImagPart;
        double iP = first.RealPart * second.ImagPart + first.ImagPart * second.RealPart;
        return new ComplexNumber(rP, iP);
    }
    public static ComplexNumber operator /(ComplexNumber first, ComplexNumber second)
    {
        double rP = (first.RealPart * second.RealPart + first.ImagPart * second.ImagPart) / 
                 (second.RealPart * second.RealPart + second.ImagPart + second.ImagPart);
        double iP = (first.RealPart * second.ImagPart - first.ImagPart * second.RealPart) /
                 (second.RealPart * second.RealPart + second.ImagPart + second.ImagPart);
        return new ComplexNumber(rP, iP);
    }
    public static bool operator ==(ComplexNumber first, ComplexNumber second)
    {
        if (first.RealPart != second.RealPart)
        {
            return false;
        }
        else
        {
            if (first.ImagPart != second.ImagPart)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
    public static bool operator !=(ComplexNumber first, ComplexNumber second)
    {
        return !(first == second);
    }
    public static implicit operator ComplexNumber(double number)
    {
        return new ComplexNumber(number, 0);
    }
    public static explicit operator double(ComplexNumber number)
    {
        if (number.ImagPart != 0)
        {
            return double.NaN;
        }
        else
        {
            return number.RealPart;
        }
    }

    public double this[int index]
    {
        get 
        {
            if (index < 0 || index > 1)
            {
                throw new IndexOutOfRangeException(string.Format("Index {0} is out of range.", index));
            }
            else
            {
                if (index == 0)
                {
                    return this.realPart;
                }
                else
                {
                    return this.imagPart;
                }
            }
        }
        set
        {
            if (index < 0 || index > 1)
            {
                throw new IndexOutOfRangeException(string.Format("Index {0} is out of range.", index));
            }
            else
            {
                if (index == 0)
                {
                    realPart = value;
                }
                else
                {
                    imagPart = value;
                }
            }
        }
    }
    public double RealPart
    {
        get
        {
            return realPart;
        }
        set
        {
            realPart = value;
        }
    }
    public double ImagPart
    {
        get
        {
            return imagPart;
        }
        set
        {
            imagPart = value;
        }
    }

    public override string ToString()
    {
        return string.Format("({0}, {1}i)", realPart, imagPart);
    }
}

